<?php
namespace V1;
class MyRank extends  \ResourceClass{
    public $isActive = true;
    public $remark = '我的积分排名(总-周)';

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require uid ';
        }
        $checkRes = $this->autoCheck($req['uid']);
        if($checkRes['code'] !== 200){
            return $checkRes['message'];
        }
        $week_type = $req['week_type']??'';
        try {
            /*获取用户 总排名-总积分*/
            $order = ' score DESC, insert_time ASC';
            $user_account_list = new \Orm\UserAccount\UserAccountList();
            $user_account_list['order'] = $order;
            $user_account_list->load();
            $tmp['total_sort'] = $tmp['total_score'] = 0;
            if(count($user_account_list['lists']) > 0){
                foreach($user_account_list['lists'] as $key=>$val){
                    if($val['uid'] == $req['uid']){
                        $tmp['total_sort'] = $key + 1;
                        $tmp['total_score'] = $val['score'];
                        break;
                    }
                }
            }
            /*获取周排名-周积分*/
            $get_week_type = \Orm\UserAccount\UserAccountClass::get_week($week_type);
            $order_week = ' '.$get_week_type.' DESC , insert_time ASC';

            $user_account_list = new \Orm\UserAccount\UserAccountList();
            $user_account_list['order'] = $order_week;
            $user_account_list->load();
            $tmp['sort'] = $tmp['score'] = 0;
            if(count($user_account_list['lists']) > 0){
                foreach($user_account_list['lists'] as $key=>$val){
                    if($val['uid'] == $req['uid']){
                        $tmp['sort'] = $key + 1;
                        $tmp['score'] = $val['score'];
                        break;
                    }
                }
            }
            $result = $tmp;
        }catch (\Exception $e) {
            $result = array(
                'total_sort'=>0,
                'total_score'=>0,
                'sort'=>0,
                'score'=>0,
            );
        }
    }
}